package databasetest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class UpdateG extends JInternalFrame {
	Connection conn;
	GuestBookMain gbm;
	PreparedStatement pstmt=null;
	SelectG s;
	
	private JLabel lblNewLabel;
	private JTextField serial;
	private JLabel lblNewLabel_1;
	private JTextField user;
	private JLabel lblNewLabel_2;
	private JTextField jemok;
	private JScrollPane scrollPane;
	private JTextArea comment;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JLabel lblNewLabel_3;
	private JTextField date;


	public UpdateG(Connection conn, GuestBookMain gbm) {
		super("방명록 수정 및 삭제",false,true,true,false);
		this.conn=conn;
		this.gbm=gbm;
		setBounds(100, 100, 448, 428);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getSerial());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getUser());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getJemok());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getLblNewLabel_3());
		getContentPane().add(getDate());
		

	}
	public void setU(SelectG u){
		this.s=u;
	}
	
	public void Modify(){
		
		int s= Integer.parseInt(serial.getText());
		String c= user.getText();
		String e= jemok.getText();
		String l= comment.getText();
		
		String sql= "update guestbook set mid=?, subject=?,content=?  where serial=?";
		try{
			pstmt= conn.prepareStatement(sql);
			pstmt.setString(1, c);
			pstmt.setString(2, e);
			pstmt.setString(3, l);
			pstmt.setInt(4, s);
			
			pstmt.executeUpdate();
			pstmt.close();
		}catch(Exception ex){
		}
		
	}
	
	public void dele(){
		int s= Integer.parseInt(serial.getText());
		String sql= "delete from guestbook where serial=?";
		try{
			pstmt= conn.prepareStatement(sql);
			pstmt.setInt(1, s);
			
			pstmt.executeUpdate();
			pstmt.close();
			serial.setText("");
			user.setText("");
			jemok.setText("");
			date.setText("");
			comment.setText("");
			
		}catch(Exception ex){
			ex.getMessage();
			
		}
	}
	
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("No");
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setEnabled(false);
			serial.setBounds(81, 7, 86, 21);
			serial.setColumns(10);
		}
		return serial;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("작성자");
			lblNewLabel_1.setBounds(12, 35, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getUser() {
		if (user == null) {
			user = new JTextField();
			user.setBounds(81, 32, 86, 21);
			user.setColumns(10);
		}
		return user;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("제목");
			lblNewLabel_2.setBounds(12, 66, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getJemok() {
		if (jemok == null) {
			jemok = new JTextField();
			jemok.setBounds(81, 63, 341, 21);
			jemok.setColumns(10);
		}
		return jemok;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 91, 408, 235);
			scrollPane.setViewportView(getComment());
		}
		return scrollPane;
	}
	private JTextArea getComment() {
		if (comment == null) {
			comment = new JTextArea();
		}
		return comment;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("수정");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					Modify();
					s.columnadd();
				}
			});
			btnNewButton.setBounds(260, 336, 74, 36);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("삭제");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					dele();
					s.columnadd();
				}
			});
			btnNewButton_1.setBounds(346, 336, 74, 36);
		}
		return btnNewButton_1;
	}
	
	
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("작성일");
			lblNewLabel_3.setBounds(179, 10, 57, 15);
		}
		return lblNewLabel_3;
	}
	private JTextField getDate() {
		if (date == null) {
			date = new JTextField();
			date.setEnabled(false);
			date.setBounds(238, 7, 184, 21);
			date.setColumns(10);
		}
		return date;
	}
	
	
	
	public void setData(String a, String b, String c,String d,String e){
		serial.setText(a);
		user.setText(b);
		jemok.setText(c);
		comment.setText(d);
		date.setText(e);
	}
}
